import pandas as pd
from quant_researcher.quant.project_tool.celebrity import union_filter
import ast


tag = "stablecoin"
path = "G:/data/coinmarketcap"
coins_df = pd.read_csv(f'{path}/all_coin_list.csv')
tags_df = pd.read_csv(f'{path}/coin_tags.csv')
# print(tags_df.dtypes)
tags_df["tags"] = tags_df["tags"].apply(ast.literal_eval)
# print(tags_df.dtypes)
# 币的信息和标签信息合并
all_df = coins_df.merge(tags_df[['id', 'tags']], how='left', on=['id'])

# 所有支持的tags
all_tags_list = []
index_num_list = []
for i, this_tags in enumerate(all_df['tags']):
    if this_tags == []:
        pass
    elif str(this_tags) == 'nan':
        pass
    else:
        all_tags_list.append(this_tags)
        if tag == 'all':
            index_num_list = list(all_df.index)
        else:
            if tag.lower() in this_tags:
                index_num_list.append(i)

all_tags_list = union_filter(all_tags_list, how_to_deal='or')
all_tags_list.sort()
all_tags = pd.Series(all_tags_list)

all_df = all_df.iloc[index_num_list, :]
all_df.set_index('id', inplace=True)
all_df.sort_index(inplace=True)
all_df.to_csv(f'{path}/all_stablecoin_list_coinmarketcap.csv')
